package com.yitongmao.bottompop;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.yitongmao.yitmfruit.R;

import java.util.Hashtable;

/**
* Created by Administrator on 2016/9/23.
* PopupWindow工具类
*/

public class PopupWindowUtil {

   private static Hashtable<String, PopupWindow> hashtable = new Hashtable<>();
   private static int width;
   private static PopupWindow pw;

   /**
    * 切换popuWindow显示/隐藏
    */

   public static View togglePopup(String className, int id, View view) {
       //TODO 写死了大小
       return togglePopup(className, id, view, 460, 320, 35, 30);
   }

   /**
    * 切换popuWindow显示/隐藏,根据传入值设置大小
    * @param className 不同类的标记
    * @param id        需要显示的布局id
    * @param view      显示在view之下
    * @param width     显示宽度
    * @param height    显示高度
    * @param x         x方向偏移量
    * @param y         y方向偏移量
    */
   public static View togglePopup(String className, int id, View view, int width, int height, int x, int y) {
       PopupWindow mPopupWindow = hashtable.get(className);

       if (mPopupWindow == null) {
           View contentView = UIUtils.inflate(id);
           mPopupWindow = new PopupWindow(contentView, width, height, true);
           mPopupWindow.setTouchable(true);
           mPopupWindow.setOutsideTouchable(true);
           mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
           hashtable.put(className, mPopupWindow);
       }
       if (mPopupWindow.isShowing()) {
           mPopupWindow.dismiss();
           //mPopupWindow = null;
       } else {
           mPopupWindow.showAsDropDown(view, x, y);
       }
       return mPopupWindow.getContentView();
   }

   /**
    * 显示在控件下方
    *
    * @param className
    * @param id
    * @param view
    * @param y
    * @return
    */
   public static View togglePopup(String className, int id, View view, int y) {
       PopupWindow mPopupWindow = hashtable.get(className);
       if (mPopupWindow == null) {
           View contentView = UIUtils.inflate(id);
           contentView.measure(0, 0);
           width = contentView.getMeasuredWidth();
           int height = contentView.getMeasuredHeight();
           mPopupWindow = new PopupWindow(contentView, width, height, true);
           mPopupWindow.setTouchable(true);
           mPopupWindow.setOutsideTouchable(true);
           mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
           hashtable.put(className, mPopupWindow);
       }
       if (mPopupWindow != null && mPopupWindow.isShowing()) {
           mPopupWindow.dismiss();
               mPopupWindow = null;
       } else {
           if (!mPopupWindow.isShowing())
               mPopupWindow.showAsDropDown(view, -width * 3 / 4, y);
       }
       return mPopupWindow.getContentView();
   }

   /**
    * 从底部弹出
    * @param activity
    * @param className
    * @param view
    */
   public static View toogleInButton(final Activity activity, final String className, View view, int id) {
       pw = hashtable.get(className);
       if (pw == null) {
           View contentView = UIUtils.inflate(id);
           contentView.measure(0, 0);
           int height = contentView.getMeasuredHeight();
           pw = new PopupWindow(contentView, activity.getWindowManager().getDefaultDisplay().getWidth(), height, true);
           pw.setAnimationStyle(R.style.pw);
           pw.setOutsideTouchable(true);
           pw.setTouchable(true);
           hashtable.put(className, pw);
       }
       if (pw != null && pw.isShowing()) {
           pw.dismiss();
       } else {
           if (pw != null && !pw.isShowing()) {
               pw.showAtLocation(view, Gravity.BOTTOM, 0, 0);
               showOrHidShade(activity,0.5f);
           }
       }
       pw.setOnDismissListener(new MyPopupCancelListener(activity));
       return pw.getContentView();
   }

   /**
    * 显示/隐藏遮罩层
    * @param activity
    */
   public static void showOrHidShade(Activity activity, float alpha) {
       Window window = activity.getWindow();
       WindowManager.LayoutParams attributes = window.getAttributes();
       attributes.alpha = alpha;
       window.setAttributes(attributes);
   }

   public static void cancel(String className) {
       PopupWindow popupWindow = hashtable.get(className);
       if (popupWindow != null && popupWindow.isShowing()) {
           popupWindow.dismiss();
       }
   }

}
